<template>
  <div class="profile">
    <!-- 未登录 -->
    <div class="header not-login" v-if="!user">
      <img src="../../assets/images/mobile.png" alt="" @click="$router.push('/Login')">
      <span>登录 / 注册</span>
    </div>
    <!-- 已登录 -->
    <div class="header login-box" v-else>
      <!-- 上面 -->
      <div class="user-info">
        <!-- 头像 -->
        <div class="left">
          <img :src="userInfo.photo" alt="">
          <span>{{userInfo.name}}</span>
        </div>
        <!-- 更多资料 -->
        <van-button type="default" to="/User">编辑资料</van-button>
      </div>
      <!-- 下面 -->
      <div class="user-data">
        <div class="data-item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <van-grid class="myGrid" :column-num="2">
      <van-grid-item text="文字">
        <template #icon>
          <i class="iconfont toutiao-shoucang"></i>
        </template>
      </van-grid-item>
      <van-grid-item text="文字">
        <template #icon>
          <i class="iconfont toutiao-lishi"></i>
        </template>
      </van-grid-item>
    </van-grid>
    <div class="out-box">
      <van-cell title="单元格" is-link />
      <van-cell title="单元格" is-link />
    </div>
      <van-cell @click="tuChu" title="退出" class="login-btn" v-if="user"/>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { Dialog, Toast } from 'vant'
import { getUserSelfInfo } from '../../api/user.js'
export default {
  data () {
    return {
      userInfo: ''
    }
  },

  created () {
    this.loadUserInfo()
  },
  computed: {
    ...mapState(['user'])
  },

  methods: {
    tuChu () {
      Dialog.confirm({
        title: '黑马头条',
        message: '是否确认退出'
      })
        .then(() => {
          this.$store.commit('rumoveUer')
        })
        .catch(() => {
        })
    },
    async loadUserInfo () {
      try {
        // this.user 里就包含了，token 登录的用户信息
        const res = await getUserSelfInfo()
        console.log(res)
        this.userInfo = res
      } catch (err) {
        Toast('获取失败')
      }
    }
  }
}
</script>

<style scoped lang= 'less'>
.profile {
  .header {
    width: 750px;
    height: 401px;
    background-image: url(~@/assets/images/banner.png);
    background-size: 100% 100%;
  }
  /* 登录区 */
  .not-login {
    display: flex;
    flex-direction: column;
    justify-content:center;
    align-items:center;
    img {
      width: 132px;
      height: 132px;
      margin-bottom: 10px;
    }
  }
  /* 未登录 */
  .login-box {
    overflow: hidden;
    /* 头像区 */
    .user-info {
      width: 100%;
      height: 132px;
      margin-top: 116px;
      padding: 0 32px;
      box-sizing: border-box;
      display: flex;
      justify-content:space-between;
      align-items:center;
      .left {
        display: flex;
        align-items:center;
        img {
          width: 132px;
          height: 132px;
          border-radius: 50%;
          border: 1px solid #fff;
        }
        span {
          margin-left: 22px;
          color: #fff;
          font-size: 30px;
        }
      }
      .van-button {
        width: 115px;
        height: 32px;
        background: #FFFFFF;
        border-radius: 16px;
        font-size: 20px;
        color: #666;
        padding: 0;
      }
    }
    /* 分数区 */
    .user-data {
      width: 100%;
      height: 156px;
      display: flex;
      .data-item {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        color: #fff;

        span:nth-child(1) {
          font-size: 26px;
          margin-bottom: 6px;
        }
        span:nth-child(2) {
          font-size: 22px;
        }
      }
    }
  }
  .myGrid {
    .iconfont {
      font-size: 45px;
    }

    .toutiao-shoucang {
      color: #EB5253;
    }

    .toutiao-lishi {
      color: #FF9D1D;
    }

  }
  .out-box {
    margin: 10px 0;
  }
  .login-btn {
    text-align: center;
    color: #f00;
  }
}

</style>
